﻿using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;

namespace Journeo_backend.Services.Implementations
{
    public class RedisService
    {

        private readonly IConnectionMultiplexer _redis;

        public RedisService(IConnectionMultiplexer redis)
        {
            _redis = redis;
        }

        // 上传图片到Redis（图片以base64格式存储）
        public async Task<Response<string>> UploadImageAsync(string key, string base64Image)
        {
            try
            {
                var db = _redis.GetDatabase();
                await db.StringSetAsync(key, base64Image);
                return new Response<string>("success", Messages.REDIS_UPLOAD_SUCCESS, key);
            }
            catch (Exception e)
            {
                return await Task.FromResult(new Response<string>("error", e.Message));
            }
        }

        // 从Redis获取图片
        public async Task<Response<string>> GetImageAsync(string key)
        {
            try
            {
                var db = _redis.GetDatabase();
                var base64Image = await db.StringGetAsync(key);
                return new Response<string>("success", Messages.REDIS_GET_SUCCESS, base64Image);
            }
            catch (Exception e)
            {
                return await Task.FromResult(new Response<string>("error", e.Message));
            }
        }

        // 从Redis删除图片
        public async Task<Response<string>> DeleteImageAsync(string key)
        {
            try
            {
                var db = _redis.GetDatabase();
                await db.KeyDeleteAsync(key);
                return new Response<string>("success", Messages.REDIS_DELETE_SUCCESS);
            }
            catch (Exception e)
            {
                return await Task.FromResult(new Response<string>("error", e.Message));
            }
        }

        // 上传图片到Redis（以文件类型为参数，图片以base64格式存储）
        public async Task<Response<string>> UploadImageAsync(string key, IFormFile file)
        {
            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);
                    var fileBytes = memoryStream.ToArray();
                    var base64Image = Convert.ToBase64String(fileBytes);

                    var db = _redis.GetDatabase();
                    await db.StringSetAsync(key, base64Image);
                    return new Response<string>("success", Messages.REDIS_UPLOAD_SUCCESS, key);
                }
            }
            catch (Exception e)
            {
                return await Task.FromResult(new Response<string>("error", e.Message));
            }
        }

        // 上传临时验证码到Redis
        // SetAsync(redisKey, verificationCode, TimeSpan.FromMinutes(5))
        public async Task<bool> SetVerificationCodeAsync(string key, string verificationCode)
        {
            try
            {
                var db = _redis.GetDatabase();
                await db.StringSetAsync(key, verificationCode, TimeSpan.FromMinutes(5));
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        // 获取临时验证码
        public async Task<string> GetVerificationCodeAsync(string key)
        {
            try
            {
                var db = _redis.GetDatabase();
                return await db.StringGetAsync(key);
            }
            catch (Exception e)
            {
                return null;
            }
        }

        // 删除临时验证码
        public async Task<bool> DeleteVerificationCodeAsync(string key)
        {
            try
            {
                var db = _redis.GetDatabase();
                return await db.KeyDeleteAsync(key);
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
